package com.bangyu.monitor.modular.system.entity;

import cn.hutool.core.date.DateTime;
import com.bangyu.monitor.core.util.FormatUtil;
import com.bangyu.monitor.modular.system.model.MaintainRecordDto;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.util.Strings;

import java.util.List;

@Data
@TableName("sys_maintain_record")
public class MaintainRecord {


    @TableId(value = "id", type = IdType.ID_WORKER)
    private Long id;
    //桥id
    private Long bridgeId;
    //梁编码
    private String beamNo;
    //维修日期
    private Integer maintainDate;
    //填表人
    private String person;
    //维修类型，以逗号分隔
    private String type;


    public static MaintainRecord dtoToModule(MaintainRecordDto dto) {
        if (dto == null) {
            return null;
        }
        MaintainRecord maintainRecord = new MaintainRecord();
        maintainRecord.setId(dto.getId());
        maintainRecord.setBridgeId(dto.getBridgeId());
        maintainRecord.setBeamNo(dto.getBeamNo());
        maintainRecord.setMaintainDate(FormatUtil.parseDateStr2Int(dto.getMaintainDate()));
        maintainRecord.setPerson(dto.getPerson());
        List types = dto.getTypes();
        if(CollectionUtils.isNotEmpty(types)){
            CollectionUtils.filter(types, new Predicate() {
                @Override public boolean evaluate(Object object) {
                    return !(object==null|| Strings.EMPTY.equalsIgnoreCase(object.toString()));
                }
            });
            maintainRecord.setType(StringUtils.join(types,","));
        }else{
            maintainRecord.setType("");
        }
        return maintainRecord;
    }

}